﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Jamhee.Noctiluca.Common;
using System.IO;

namespace Jamhee.Noctiluca.WebServer
{
    public abstract class WebConfiguration : IWebConfiguration
    {
        #region Defined constants

        protected const string INI_SECTION_APPLICATION = "APPLICATION";
        protected const string INI_SECTION_APPLICATION_KEY_FILE = "file";
        protected const string INI_SECTION_APPLICATION_KEY_ASSEMBLY = "assembly";
        protected const string INI_SECTION_WEBSITE = "WEBSITE";
        protected const string INI_SECTION_WEBSITE_KEY_ROOTPATH = "rootpath";
        
        #endregion

        #region Variables
        protected string _fileName = "";
        protected IniFile _iniFile = null;
        #endregion

        public WebConfiguration(string filename)
        {
            _fileName = filename;

            string iniFilePath = AppUtil.GetAbsolutePath(AppUtil.CONFIG_FILE_PATH + "\\" + filename);
            _iniFile = new IniFile(iniFilePath);

            LogUtil.PrintInfo("Use configuration file: " + iniFilePath);
        }

        public string GetAppFile()
        {
            if (_iniFile != null && _iniFile.Exist())
            {
                return _iniFile.IniReadValue(INI_SECTION_APPLICATION, INI_SECTION_APPLICATION_KEY_FILE);
            }

            return string.Empty;
        }

        public string GetAppAssemblyName()
        {
            if (_iniFile != null && _iniFile.Exist())
            {
                return _iniFile.IniReadValue(INI_SECTION_APPLICATION, INI_SECTION_APPLICATION_KEY_ASSEMBLY);
            }

            return string.Empty;
        }

        public string GetWebsiteRootPath()
        {
            if (_iniFile != null && _iniFile.Exist())
            {
                return _iniFile.IniReadValue(INI_SECTION_WEBSITE, INI_SECTION_WEBSITE_KEY_ROOTPATH);
            }

            return string.Empty;
        }

        public static IWebConfiguration GetWebConfiguration(string webConfigName)
        {
            IWebConfiguration webConfig = null;
            IniFile iniFile = new IniFile(AppUtil.GetAbsolutePath(AppUtil.CONFIG_FILE_PATH + "\\" + webConfigName));

            if (iniFile.Exist())
            {
                // Get dll file name and class name
                string dllFile = iniFile.IniReadValue(INI_SECTION_APPLICATION, INI_SECTION_APPLICATION_KEY_FILE);
                dllFile = AppUtil.GetAbsolutePath(AppUtil.EXECUTION_FILE_PATH + "\\" + dllFile);

                string assembly = iniFile.IniReadValue(INI_SECTION_APPLICATION, INI_SECTION_APPLICATION_KEY_ASSEMBLY);

                if (File.Exists(dllFile) && assembly.Trim().Length > 0)
                {
                    try
                    {
                        // Create instance
                        webConfig = (IWebConfiguration)Assembly.LoadFile(dllFile).CreateInstance(assembly, false, 
                            BindingFlags.CreateInstance, null, new object[] { webConfigName }, null, null);
                        LogUtil.PrintInfo("Web configuration [" + webConfigName + "] was successfully loaded");
                    }
                    catch (Exception e)
                    {
                        LogUtil.PrintError("An error happened when loading web configuration [" + webConfigName + "]:" + e.Message);
                    }
                }
            }

            return webConfig;
        }

        public abstract IWebResponser GetWebResponser();

        #region Properties

        public string ConfigurationFileName
        {
            get { return _fileName; }
        }

        #endregion
    }
}
